package m.jclecai.com.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;

import m.jclecai.com.R;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.SelectListHolder;
import m.jclecai.com.dialog.SelectlistDialog;

/**
 * Created by ZD on 2017/5/24.
 */

public class SelectListAdapter extends ViewHolerAdapter<SelectListHolder> {

    JSONArray jsonArray;
    Context mContext;
    SelectlistDialog.TYPE type;
    String selectName = "";

    public SelectListAdapter(Context context, JSONArray jsonArray,SelectlistDialog.TYPE type,String selectName) {
        this.jsonArray = jsonArray;
        this.mContext = context;
        this.type = type;
        this.selectName = selectName;
    }

    public void setIndex(String selectName){
        this.selectName = selectName;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return jsonArray.get(position);
    }

    @Override
    public SelectListHolder onCreateViewHolder(int position, ViewGroup parent) {
        return new SelectListHolder(mContext, R.layout.item_dialog_select);
    }

    @Override
    public void onBindViewHolder(int position, SelectListHolder selectListHolder) {
        String string = jsonArray.getString(position);
        if(string.equals(selectName) || (position == 0 && TextUtils.isEmpty(selectName))) {
            selectListHolder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.font_red));
            selectListHolder.getView().setBackgroundColor(ContextCompat.getColor(mContext, R.color.font_white));
        }else {
            selectListHolder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.a));
            if(!type.equals(SelectlistDialog.TYPE.CITY))
            selectListHolder.getView().setBackgroundColor(ContextCompat.getColor(mContext, R.color.LotteryBg));
        }
        selectListHolder.tvName.setText(string);
    }

    @Override
    public int getCount() {
        return jsonArray.size();
    }
}
